package com.watson.onebox.aspect;

import com.watson.onebox.aspect.annotations.Login;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class AspectDemo implements EnvironmentAware {

    Environment environment;

    //切入点为使用了Login注解的任何地方
    @Pointcut("@annotation(com.watson.onebox.aspect.annotations.Login)")
    public void logPointCut() {

    }

    @Before(value = "logPointCut()")
    public void check(){
        log.info("check");
    }


    @After(value = "logPointCut()")
    public void bye(){
        log.info("bye");
    }



    @Around("logPointCut() && @annotation(login)")
    public Object around(ProceedingJoinPoint joinPoint, Login login){
//        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "/Users/zhangpeng60/Downloads/workspace");

        log.info("around");
        //输出使用login注解时候的username属性值
        String s = environment.resolvePlaceholders(login.username());
        String[] activeProfiles = environment.getActiveProfiles();
        log.info("active:{}", activeProfiles);
        log.info("username: {}", login.username());
        log.info("username-resolve: {}", s);
        //输出使用login注解时候的passwo属性值
        log.info("password: {}", login.password());
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        log.info("============================================");
        return proceed;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
